package com.lehuo.jyn;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lehuo.util.BbsUtil;
import com.lehuo.util.ConnUtil;
import com.lehuo.util.RepairUtil;
import com.lehuo.util.StringUtil;

public class LehuoBbsServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("doGet");
		doPost(req, resp);
	}

	@SuppressWarnings("unused")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		if (req == null) {
			return;
		}
		String action = req.getParameter("action");
		System.out.println("doPost action:" + action);
		switch (action) {
		case "addBbs":
			addBbs(req, resp);
			break;
		case "addBbsImg": 
			addBbsImg(req, resp);
			break;
		case "queryBbs":
			queryBbs(req, resp);
			break;
		case "deleteBbs":
			deleteBbs(req, resp);
			break;
		case "getBbs":
			getBbs(req, resp);
			break;
		case "addBbscom":
			addBbscom(req, resp);
			break;
		case "getBbscom":
			getBbscom(req, resp);
			break;
		default:
			break;
		}
	}

	public void addBbs(HttpServletRequest req, HttpServletResponse resp) {
		String user_id = req.getParameter("user_id");
		String add_time = req.getParameter("add_time");
		String bbs_title = req.getParameter("bbs_title");
		String bbs_content = req.getParameter("bbs_content");
		String bbs_type = req.getParameter("bbs_type");

		user_id = StringUtil.toutf(user_id);
		add_time = StringUtil.toutf(add_time);
		bbs_title = StringUtil.toutf(bbs_title);
		bbs_content = StringUtil.toutf(bbs_content);
		bbs_type = StringUtil.toutf(bbs_type);

		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = BbsUtil.addBbs(user_id, add_time, bbs_title,
					bbs_content, bbs_type, connection);
			System.out.println("addRepair result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void queryBbs(HttpServletRequest req, HttpServletResponse resp) {
		Connection connection = null;
		connection = ConnUtil.getConnection();
		try {
			connection.setAutoCommit(false);
			String result = BbsUtil.queryBbs(connection);
			System.out.println("queryBbs result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void getBbs(HttpServletRequest req, HttpServletResponse resp) {
		String user_id = req.getParameter("user_id");
		user_id = StringUtil.toutf(user_id);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = BbsUtil.getBbs(user_id, connection);
			System.out.println("getBbs result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void addBbsImg(HttpServletRequest req, HttpServletResponse resp) {
		String bbs_id = req.getParameter("bbs_id");
		String bbs_img = req.getParameter("bbs_img");
		bbs_id = StringUtil.toutf(bbs_id);
		bbs_img = StringUtil.toutf(bbs_img);
		
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = BbsUtil.addBbsImg(bbs_id, bbs_img, connection);
			System.out.println("addBbsImg result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteBbs(HttpServletRequest req, HttpServletResponse resp){
		String id = req.getParameter("id");
		id = StringUtil.toutf(id);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = BbsUtil.deleteBbs(id, connection);
			System.out.println("addRepair result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void addBbscom(HttpServletRequest req, HttpServletResponse resp) {
		String bbs_id = req.getParameter("bbs_id");
		String user_id = req.getParameter("user_id");
		String com_des = req.getParameter("com_des");
		bbs_id = StringUtil.toutf(bbs_id);
		user_id = StringUtil.toutf(user_id);
		com_des = StringUtil.toutf(com_des);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = BbsUtil.addBbscom(bbs_id, user_id, com_des, connection);
			System.out.println("addBbscom result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void getBbscom(HttpServletRequest req, HttpServletResponse resp) {
		String bbs_id = req.getParameter("bbs_id");
		bbs_id = StringUtil.toutf(bbs_id);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = BbsUtil.getBbscom(bbs_id, connection);
			System.out.println("addBbscom result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
